package com.api.controller;


import com.api.common.exception.BaseException;
import com.api.model.dto.user.*;
import com.api.model.entity.User;
import com.api.model.reslut.Result;
import com.api.model.reslut.ResultPage;
import com.api.model.vo.UserVo;
import com.api.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
@Slf4j
public class LogInController {

    @Resource
    private UserService userService;

    /**
     * 登录
     * @param session
     * @return
     */
    @PostMapping("/login")
    private Result<UserVo> login(@RequestBody UserLoginDTO userLoginDTO, HttpSession session) {
        User user = userService.login(userLoginDTO);

        if (ObjectUtils.isEmpty(user)) {
            throw new BaseException("用户不存在");
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        session.setAttribute("user", userVo);
        return Result.success(userVo);
    }


    // 注册
    @PostMapping("/register")
    private Result<UserVo> Register(@RequestBody RegisterdDTO registerdDTO) {
        User user = userService.register(registerdDTO);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        return Result.success(userVo);
    }


    /**
     * 获取登录用户
     * @param session
     * @return
     */
    @GetMapping("/get/login")
    private Result<UserVo> getLogin(HttpSession session) {
        log.info("获取登录用户");
        UserVo userVo = (UserVo )session.getAttribute("user");
        if (ObjectUtils.isEmpty(userVo)){
            return Result.faile("未登录");
        }
        return Result.success(userVo);
    }


    // 退出
    @GetMapping("/logout")
    private Result<Boolean> logout(HttpSession session) {
        UserVo userVo = (UserVo )session.getAttribute("user");
        if (ObjectUtils.isEmpty(userVo)){
            return Result.faile("未登录");
        }
        session.removeAttribute("user");
        return Result.success(true);
    }


    // 添加
    @PostMapping("/addUser")
    private Result<UserVo> addUser(@RequestBody UserAddDTO userAddDTO) {
        User user = new User();
        BeanUtils.copyProperties(userAddDTO, user);
        userService.save(user);
        return Result.success();
    }


    //修改
    @PostMapping("/updateUser")
    private Result<UserVo> updateUser(@RequestBody UserUpdateDTO userUpdateDTO) {
        User user = new User();
        BeanUtils.copyProperties(userUpdateDTO, user);
        userService.updateById(user);
        return Result.success();
    }


    //查询列表
    @PostMapping("/getlist")
    private Result<ResultPage> getlist(@RequestBody UserQueryDTO userQueryDTO) {
        ResultPage listPage = userService.getListPage(userQueryDTO);
        return Result.success(listPage);
    }

    //查询一个
    @PostMapping("/getUser/{id}")
    private Result<UserVo> getUser(@PathVariable Integer id) {
        User user = userService.getById(id);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        return Result.success(userVo);
    }



    // 删除
    @GetMapping("/delete/{id}")
    private Result<Boolean> deleteUser(HttpSession session, @PathVariable Integer id) {
        userService.removeById(id);
        return Result.success(true);
    }
}
